package com.xiaoxin.experience.service.ssh;

import com.xiaoxin.experience.service.ssh.model.SSHSessionInfo;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author xiaoxin
 */
public class SSHJClientManager
{
    private SSHJClientManager() {}

    private static final Map<String,SSHJClient> SSHJ_CLIENT_MAP = new ConcurrentHashMap<>();

    public static SSHJClient getSSHJClient(SSHSessionInfo sshSessionInfo)
            throws IOException
    {
        SSHJClient sshjClient = SSHJ_CLIENT_MAP.get(sshSessionInfo.getIp());
        if (Objects.isNull(sshjClient))
        {
            sshjClient = new SSHJClient(sshSessionInfo);
            SSHJ_CLIENT_MAP.put(sshSessionInfo.getIp(),sshjClient);
        }
        return sshjClient;
    }

    public static void removeSSHJClient(String ip)
    {
        SSHJClient remove = SSHJ_CLIENT_MAP.remove(ip);
        if (Objects.nonNull(remove))
        {
            try
            {
                remove.release();
            }
            catch (Exception ignore){}
        }
    }
}
